package com.pug.controller.hotel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pug.bo.HotelBo;
import com.pug.bo.HotelTypeBo;
import com.pug.controller.APIBaseController;
import com.pug.service.hotel.IHotelService;
import com.pug.service.hotel.category.IHotelTypeService;
import com.pug.utils.JsonUtil;
import com.pug.utils.fn.asserts.Vsserts;
import com.pug.vo.HotelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/3/19$ 16:59$
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@Api(tags = "酒店管理")
public class HotelController extends APIBaseController {

    private final IHotelService hotelService;
    private final IHotelTypeService hotelTypeService;

    /**
     * 查询和分页对应的酒店信息
     *
     * @param hotelVo
     * @return
     */
    @PostMapping("/hotel/load")
    @ApiOperation("酒店列表分页查询")
    public IPage<HotelBo> searchHotels(@RequestBody HotelVo hotelVo) {
        return hotelService.searchHotels(hotelVo);
    }


    /**
     * 查询和分页对应的酒店信息
     *
     * @param hotelId
     * @return
     */
    @PostMapping("/hotel/detail/{id}")
    @ApiOperation("酒店明细信息")
    public HotelBo getDetailHotels(@PathVariable("id") String hotelId) {
        Vsserts.isNotEmptyEx(hotelId, "酒店ID不能为空!!");
        HotelBo hotelBo = hotelService.getHotels(Long.parseLong(hotelId));
        if (Vsserts.isNotNull(hotelBo) && Vsserts.isNotEmpty(hotelBo.getImglists())) {
            List<String> stringList = new ArrayList<>();
            // 把主图放第一张
            stringList.add(hotelBo.getImg());
            String[] split = hotelBo.getImglists().split(",");
            for (String s : split) {
                stringList.add(s);
            }
            // 其他的图集往后面放
            hotelBo.setImagesList(stringList);
            // 把原来的清空
            hotelBo.setImglists("");
        }


        if (Vsserts.isNotNull(hotelBo)) {
            //处理酒店的服务项目
            String hotelServiceItem = hotelBo.getHotelServiceItem();
            if (Vsserts.isNotEmpty(hotelServiceItem)) {
                List<Map<String, Object>> serviceItemMaps = JsonUtil.string2Obj(hotelServiceItem, List.class, HashMap.class);
                serviceItemMaps = serviceItemMaps.stream().sorted((a, b) -> {
                    return (int) a.get("sort") - (int) b.get("sort");
                }).collect(Collectors.toList());
                hotelBo.setHotelServiceItem("");
                hotelBo.setServiceItems(serviceItemMaps);
            }

            // 查询对应的酒店房型
            List<HotelTypeBo> hotelTypeBos = hotelTypeService.findByHotelId(Long.parseLong(hotelId));
            if(!CollectionUtils.isEmpty(hotelTypeBos) ){
                hotelTypeBos =  hotelTypeBos.stream().peek(htype->{
                    List<String> imagesList = new ArrayList<>();
                    imagesList.add(htype.getImg());
                    String imglists = htype.getImglists();
                    if(Vsserts.isNotEmpty(imglists)){
                        String[] strings = imglists.split(",");
                        for (String string : strings) {
                            imagesList.add(string);
                        }
                    }
                    htype.setImglists("");
                    htype.setImageLists(imagesList);
                }).collect(Collectors.toList());
            }else{
                hotelTypeBos = new ArrayList<>();
            }

            hotelBo.setHotelTypes(hotelTypeBos);
        }

        return hotelBo;
    }


}
